/**
 * Created by Administrator on 2017/5/22.
 */
var async = require('async');
var dao=require('../../common/utils/appUtils')
var request = require('request');



var from = 'oSx2ps7hAjniYA1eKD5c5YdCLBco'
var fromid='50'
async.waterfall([
    function (callback) {
        dao.baseDao('','select access_token from gdas_wechat_access_token where id=1',null,function (result) {
            callback(null,result.rows[0].access_token)
        })
    },
    function (arg,callback) {
        dao.baseDao('','select member_id from member_user where openid=?',from,function (reslut) {
            if(!reslut.isSuccess||reslut.rows.length<1) {
                callback('您尚未注册会员，无法获得推广链接，请先注册')
            }else{
                callback(null, arg, reslut.rows[0].member_id);
            }
        })
    },
    function (arg, arg1,callback) {
        var url = 'https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token='+arg ;

        request({
            url: url,
            method: "POST",
            json: true,
            headers: {
                "content-type": "application/json",
            },
            body: {"expire_seconds": 604800, "action_name": "QR_SCENE", "action_info": {"scene": {"scene_id": arg1}}}
        }, function(error, response, body) {
            console.log(JSON.stringify(response));
            callback(null,body.ticket,arg)
        });
    },function (arg,token, callback) { //查看是否存在标签
        //查找标签
        var url = 'https://api.weixin.qq.com/cgi-bin/tags/get?access_token=' + token;

        request({
            url: url,
            method: "POST",
            json: true,
            headers: {
                "content-type": "application/json",
            },
            body: {
            }
        }, function (error, response, body) {
            console.log(JSON.stringify(response));

            if (body.hasOwnProperty('tags') && body.tags.length > 0) {
                var has=false;
                for (var i = 0; i < body.tags.length; i++) {
                    if (body.tags[i].name === '推广链接'+fromid) {
                        has=true;
                        tagid = body.tags[i].id;
                        // callback(null, tagid)
                    } else {

                    }
                }
                // 没有标签，新建标签
                if (!has) {
                    var url = 'https://api.weixin.qq.com/cgi-bin/tags/create?access_token='+token ;
                    request({
                        url: url,
                        method: "POST",
                        json: true,
                        headers: {
                            "content-type": "application/json",
                        },
                        body: {  "tag" : {
                            "name" : '推广链接'+fromid//标签名
                        }}
                    }, function(error, response, body) {
                        console.log(JSON.stringify(response));
                        callback(null,arg)
                    });
                }else{
                    callback(null,arg)
                }
            }
        });
    }
],function (err, result){
    if(result) {
        let url = '<a href="https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket='+result+'">点击获取推广链接</a> ';
        console.log(url);
        //添加标签
        res.reply(url);
    }
})


